﻿namespace Uber.Data
{
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using Core.Repository;
    using System;
    using Core.Model;

    public class PayRequestRepo : BaseRepo, IPayRequestRepo
    {
        public PayRequestRepo(IConnectionFactory connFactory) : base(connFactory)
        {
        }

        public string Insert(int amount, DateTime timp, long accountId, string email, char tip)
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "insertPayRequest";
                    cmd.Parameters.Add("amount", SqlDbType.Int).Value = amount;
                    cmd.Parameters.Add("timp", SqlDbType.DateTime).Value = timp;
                    cmd.Parameters.Add("accountId", SqlDbType.BigInt).Value = accountId;
                    cmd.Parameters.Add("email", SqlDbType.NVarChar, 250).Value = email;
                    cmd.Parameters.Add("tip", SqlDbType.Char, 1).Value = tip;
                    conn.Open();

                    return (string)cmd.ExecuteScalar();
                }
            }
        }

       

        public IEnumerable<PayRequest> GetPage(int page, int pageSize, bool? filled, long? accountId)
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "getPayRequests";
                    cmd.Parameters.Add("pageSize", SqlDbType.Int).Value = pageSize;
                    cmd.Parameters.Add("page", SqlDbType.Int).Value = page;
                    cmd.Parameters.Add("filled", SqlDbType.Bit).Value = filled.HasValue? (object) filled : DBNull.Value;
                    cmd.Parameters.Add("accountId", SqlDbType.BigInt).Value = accountId.HasValue? (object) accountId : DBNull.Value;

                    conn.Open();

                    using (var dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            var o = new PayRequest();
                            o.Id = dr.GetInt64(0);
                            o.Amount = dr.GetInt32(1);
                            o.Timp = dr.GetDateTime(2);
                            o.ProId = dr.GetInt64(3);
                            o.Filled = dr.GetBoolean(4);

                            yield return o;
                        }
                    }
                }
            }
        }

        public int GetCount(bool? filled, long? accountId)
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "getPayRequestCount";
                    cmd.Parameters.Add("filled", SqlDbType.Bit).Value = filled.HasValue ? (object) filled : DBNull.Value;
                    cmd.Parameters.Add("accountId", SqlDbType.BigInt).Value = accountId.HasValue ? (object) accountId : DBNull.Value;

                    conn.Open();

                    return (int)cmd.ExecuteScalar();
                }
            }
        }
    }
}